package org.mv.mm.common.actions;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.mv.mm.common.CentralLookup;
import org.mv.mm.common.IRefreshable;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.util.NbBundle.Messages;

@ActionID(category = "File",
id = "org.mv.mm.common.RefreshAction")
@ActionRegistration(iconBase = "org/mv/mm/common/actions/icon_refresh.gif",
displayName = "#CTL_RefreshAction")
@ActionReferences({
  @ActionReference(path = "Menu/File", position = 1000),
  @ActionReference(path = "Toolbars/File", position = -96),
  @ActionReference(path = "Shortcuts", name = "F5")
})
@Messages("CTL_RefreshAction=Refresh")
public final class RefreshAction implements ActionListener {

  private static final Logger logger = Logger.getLogger(RefreshAction.class.getName());

  @Override
  public void actionPerformed(ActionEvent e) {
    CentralLookup cl = CentralLookup.getDefault();
    Collection<? extends IRefreshable> nodes =
            cl.lookupAll(IRefreshable.class);
    for (IRefreshable ref : nodes) {
      try {
        if (ref.canRefresh()) {
          ref.refreshChildren();
        }
      } catch (Exception ex) {
        logger.log(Level.WARNING, null, ex);
      }
    }

  }
}
